package com.senlingspace.web.controller.shop;

import java.util.*;

import com.alibaba.fastjson2.JSON;
import com.github.pagehelper.PageInfo;
import com.senlingspace.common.core.domain.entity.SysUser;
import com.senlingspace.common.utils.DateUtils;
import com.senlingspace.common.utils.poi.ExcelUtil;
import com.senlingspace.system.domain.SlkjShop;
import com.senlingspace.system.domain.dto.ShiftManagementDto;
import com.senlingspace.system.domain.vo.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.senlingspace.common.annotation.Log;
import com.senlingspace.common.core.controller.BaseController;
import com.senlingspace.common.core.domain.AjaxResult;
import com.senlingspace.common.enums.BusinessType;
import com.senlingspace.system.domain.SlkjShiftManagement;
import com.senlingspace.system.service.ISlkjShiftManagementService;
import com.senlingspace.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletResponse;

import static org.bouncycastle.asn1.cmc.CMCStatus.success;

/**
 * 员工交接班记录Controller
 *
 * @author SenlingSpace
 * @date 2024-07-24
 */
@RestController
@RequestMapping("/system/management/project")
public class SlkjShiftManagementController extends BaseController {
    @Autowired
    private ISlkjShiftManagementService slkjShiftManagementService;

    /**
     * 查询员工交接班记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:management:list')")
    @GetMapping("/list")
    public TableDataInfo list(SlkjShiftManagement slkjShiftManagement, Long deptId) {
        PageInfo<SlkjShiftManagement> pageInfo = slkjShiftManagementService.selectSlkjShiftManagementList(slkjShiftManagement, deptId);
        return getDataTable(pageInfo);
    }

    /**
     * 导出员工交接班记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:management:export')")
    @Log(title = "员工交接班记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SlkjShiftManagement slkjShiftManagement, Long deptId) {
        List<SlkjShiftManagement> list = slkjShiftManagementService.exportSlkjShiftManagementList(slkjShiftManagement, deptId);
        ExcelUtil<SlkjShiftManagement> util = new ExcelUtil<SlkjShiftManagement>(SlkjShiftManagement.class);
        util.exportExcel(response, list, "员工交接班记录数据");
    }

    /**
     * 获取员工交接班记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:management:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return slkjShiftManagementService.selectSlkjShiftManagementById(id);
    }

    @PreAuthorize("@ss.hasPermi('system:management:query')")
    @GetMapping("/userId")
    public AjaxResult getInfoByUserId(Long userId) {
        // todo   后续看情况加项目名称
        return slkjShiftManagementService.selectSlkjShiftManagemen(userId);
    }

    /**
     * 上岗领用
     */
    @PreAuthorize("@ss.hasPermi('system:management:work')")
    @Log(title = "员工交接班记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SlkjShiftManagement slkjShiftManagement) {
        return toAjax(slkjShiftManagementService.insertSlkjShiftManagement(slkjShiftManagement));
    }

    /**
     * 员工交班
     */
    @PreAuthorize("@ss.hasPermi('system:management:edit')")
    @Log(title = "员工交接班", businessType = BusinessType.UPDATE)
    @PutMapping
    public TableDataInfo edit(@RequestBody ShiftManagementDto pays) {
        logger.info("员工交班，入参数据：{}", JSON.toJSONString(pays));
        return getDataTable(Collections.singletonList(slkjShiftManagementService.updateSlkjShiftManagement(pays, pays.getId())));
    }

    @PreAuthorize("@ss.hasPermi('system:management:getInfo')")
    @PutMapping("/handover")
    public AjaxResult handover(Long userId) {
        return slkjShiftManagementService.handover(userId);
    }
    // /**
    //  * 删除员工交接班记录
    //  */
    // @PreAuthorize("@ss.hasPermi('system:management:remove')")
    // @Log(title = "员工交接班记录", businessType = BusinessType.DELETE)
    // @DeleteMapping("/{ids}")
    // public AjaxResult remove(@PathVariable Long[] ids)
    // {
    //     return toAjax(slkjShiftManagementService.deleteSlkjShiftManagementByIds(ids));
    // }

    //获取当前用户的id，然后得到当前用户下的门店
    @GetMapping("/getShiftManagement")
    public TableDataInfo getShiftManagement() {
        List<SlkjShopVo> slkjShopVos = slkjShiftManagementService.selectByDeptId();
        return getDataTable(slkjShopVos);
    }

    //根据前台传递的部门id查询该部门下的所有员工
    @GetMapping("/getShiftManagementByDeptId")
    public TableDataInfo getShiftManagementByDeptId(Long deptId) {
        List<SysUser> sysUsers = slkjShiftManagementService.selectUserByDeptId(deptId);
        return getDataTable(sysUsers);
    }

    //门店排序
    @GetMapping("/shopSort")
    public AjaxResult shopSort(Date startTime, Long shopId) {
        ArrayList<OrderVo> orderVos = slkjShiftManagementService.shopSort(startTime, shopId);
        return success(orderVos);
    }

    //时段分析
    @GetMapping("/period")
    public AjaxResult period(Date startTime, Long shopId) {
        return super.success(slkjShiftManagementService.period(startTime, shopId));
    }

    //营收
    @GetMapping("/revenue")
    public AjaxResult revenue(Date startTime, Long shopId) {
        logger.info("营收接口入参 startTime:{},shopId:{}", startTime, shopId);
        if (Objects.isNull(shopId)) {
            return error("门店主键不能为空！");
        }

        return success(slkjShiftManagementService.revenue(startTime, shopId));
    }

    //饼状图
    @GetMapping("/orderRatio")
    public AjaxResult orderRatio(Date startTime, Long shopId) {
        return success(slkjShiftManagementService.orderRatio(startTime, shopId));
    }

    // 员工交班结算列表
    @GetMapping("/settlement")
    public TableDataInfo settlement(SlkjShiftManagement slkjShiftManagement, Long deptId) {
        PageInfo<SlkjShiftManagement> pageInfo = slkjShiftManagementService.selectSlkjShiftManagementList(slkjShiftManagement, deptId);
        return getDataTable(pageInfo);
    }

    /**
     * 打印交班小票
     */
    @GetMapping("/receipt")
    public AjaxResult receipt(Long id) {
        if (Objects.isNull(id)) {
            return error("id不能为空！");
        }
        return success(slkjShiftManagementService.receipt(id));
    }
}
